package com.Jano.service.Impl;/**
 * @ClassName : UserServiceImpl
 * @Description :
 * @Author : Jano
 * @Date: 2022-07-24 17:04
 */

import com.Jano.dto.LoginUser;
import com.Jano.entity.User;
import com.Jano.mapper.MenuMapper;
import com.Jano.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 1.替换了UserDetailsService 实现从数据查询验证的登陆功能
 * @author zoumaoji
 * @date 2022/07/24 17:04
 **/
@Service
public class UserServiceImpl extends AbstractBaseServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    MenuMapper menuMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查询用户信息
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName,username);
        User user = userMapper.selectOne(queryWrapper);
        //如果没有查询到用户就抛出异常
        throwIfNull(user,"==>用户名或者密码错误<==");
        //TODO 查询查询对应的权限信息
        List<String> permissions = menuMapper.selectPermsByUserId(user.getId());

        //把数据封装为UserDetails返回 在loginUser中继承UserDetails接口 重写他的方法进行返回
        LoginUser loginUser = new LoginUser(user,permissions);
        return loginUser;
    }
}
